﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataTool;
using workflowMVC.Models;
using workflowMVC.Common; 

namespace workflowMVC.Controllers
{
    public class SearchController : Controller
    {
        //
        // GET: /Search/
        public ActionResult Index()
        {
            return RedirectToAction("ValidateTask");
        }

        public ActionResult ValidateTask()
        {
            wfdbEntities db = new wfdbEntities();
            List<SelectListItem> TaskKind = new List<SelectListItem>();
            SearchValidateTaskModel m = new SearchValidateTaskModel();
            m.Approver = "All";
            m.Checker = "All";
            m.TaskKind = 0;
            foreach (var r in db.TASKKINDs)
            {
                TaskKind.Add(new SelectListItem
                {
                    Value = r.Id.ToString(),
                    Text = r.Description,
                });
            }
            TaskKind.Add(new SelectListItem
            {
                Value = "0",
                Text = "Tất Cả",
            });
            ViewBag.TaskKindz = new SelectList(TaskKind, "Value", "Text");

            List<SelectListItem> Checker = new List<SelectListItem>();
            var croles = db.aspnet_Users.Where(x => x.aspnet_Roles.Select(z => z.RoleName).Contains("Checker"));
            
            foreach (var r in croles)
            {
                Checker.Add(new SelectListItem
                {
                    Value = r.UserName,
                    Text = db.STAFFs.Where(x => x.UserName == r.UserName).FirstOrDefault().RealName,
                });
            }
            Checker.Add(new SelectListItem
            {
                Value = "All",
                Text = "Tất Cả",
            });
            ViewBag.Checkerz = new SelectList(Checker, "Value", "Text");


            List<SelectListItem> Approver = new List<SelectListItem>();
            var aroles = db.aspnet_Users.Where(x => x.aspnet_Roles.Select(z => z.RoleName).Contains("Approver"));
            
            foreach (var r in aroles)
            {
                Approver.Add(new SelectListItem
                {
                    Value = r.UserName,
                    Text = db.STAFFs.Where(x => x.UserName == r.UserName).FirstOrDefault().RealName,
                });
            }
            Approver.Add(new SelectListItem
            {
                Value = "All",
                Text = "Tất Cả"
            });
            ViewBag.Approverz = new SelectList(Approver, "Value", "Text");
            
            return View(m);
        }

        [HttpPut]
        public ActionResult getSearchTableResult(SearchValidateTaskModel m)
        {
            wfdbEntities db = new wfdbEntities();
            var data = db.TASKs.AsEnumerable().Where( x => SearchUtil.IsMatchTaskKind(x.Kind.Value, m.TaskKind)
                && SearchUtil.IsMatchChecker(x.LOGs.Where(z => z.Kind == 2).FirstOrDefault(), m.Checker)
                && SearchUtil.IsMatchApprover(x.LOGs.Where(z => z.Kind == 3).FirstOrDefault(), m.Approver))
                .Select(x =>
                new SearchTaskListModel
                {
                    date = x.LOGs.Where(l => l.Kind == 1).FirstOrDefault().Date.Value,
                    id = x.Id,
                    OwnerNumcode = x.NumCode,
                    OwnerName = db.STUDENTs.Where(z => z.NumCode == x.NumCode).FirstOrDefault().Name,
                    status = x.TASKSTATU.Description,
                    taskkind = x.TASKKIND.Description,
                }
                );
            var res = data.ToList();
            res.ForEach(
            x => x.link = Util.getsTableLink(new { @class = Util.detail }, new { title = "Xem", href = Url.Action("Detail", new { id = x.id }) }));
                     
            return PartialView(res);
        }
        public ActionResult Detail(Guid Id)
        {
            wfdbEntities db = new wfdbEntities();
            var t = db.TASKs.Where(x => x.Id == Id).FirstOrDefault();
            if (t == null)
                return View("Error");
            else if (t.Kind == 1)
            {
                ViewBag.DisplayFormSourceAction = "getConfirmStuForm";                
            }
            else if (t.Kind == 2)
            {
                ViewBag.DisplayFormSourceAction = "getExcemptTuitionForm";
            }
            else if (t.Kind == 3)
            {
                ViewBag.DisplayFormSourceAction = "getStudentCardForm";
            }
            ViewBag.DisplayFormSourceController = "TaskForm";
            return View(Util.getLogsForWebPage(Id));
        }
    }
}
